package com.hxkj.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.entity.enums.FilePathEnum;
import com.hxkj.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * created by kezhijie on 2018/8/30 9:06.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 图片上传
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/imgupload", method = RequestMethod.POST)
    public Map<String, Object> imgUpload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        String src = fileService.imgUpload(file, FilePathEnum.IMG_FILE_PATH.getDescription());

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");

        Map<String, String> data = new HashMap<>();
        data.put("src", src);

        map.put("data", data);
        return map;
    }


    /**
     * ueditor  图片上传
     *
     * @param file 图片信息   第二个参数为自定义一个 存放文件的目录名称
     * @throws Exception
     */
    @RequestMapping(value = "/ueditorimgupload", method = RequestMethod.POST)
    public Callable<JSONObject> ueditorimgupload(@RequestParam(value = "file", required = false) MultipartFile file,
                                                 @RequestParam(value = "action",defaultValue = "") String action) {
        return () -> fileService.ueditorimgupload(file, FilePathEnum.IMG_FILE_PATH.getDescription(), action);
    }
}
